﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Common;
using Microsoft.Win32;


namespace my_project_energyzmem
{
    /// <summary>
    /// Логика взаимодействия для Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        public string ConnectionParameters { get; private set; }
        public SourceType DBtype { get; private set; }

        public Login()
        {
            InitializeComponent();
            ConnectionString_txbx.Text = "User Id=root; Host=10.15.176.66; Database=ptsnik2; password = vermut";
                
            XML_grbx.IsEnabled = false;
        }
/// <summary>
/// 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        private void XML_rdbtn_Checked(object sender, RoutedEventArgs e)
        {
            if (XML_rdbtn.IsChecked == true)
            {
                XML_grbx.IsEnabled = true;
                SQL_grbx.IsEnabled = false;
            }
            else
            {
               XML_grbx.IsEnabled = false;
               SQL_grbx.IsEnabled = true;
            }
            
        }
/// <summary>
/// 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        private void Cancel_btn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
/// <summary>
/// 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        private void ConnectionParam_txbx_LostFocus(object sender, RoutedEventArgs e)
        {

            if (UseWinAuth_chbx.IsChecked == true)
            {
                Login_txbx.IsEnabled = Password_txbx.IsEnabled = false;
                ConnectionString_txbx.Text = String.Format("Host={0}; Database={1}; User Id=root; password = vermut", DataSource_txbx.Text.Trim(), InitialDialog_txbx.Text.Trim());
            }
            else
            {
                Login_txbx.IsEnabled = Password_txbx.IsEnabled = true;
                ConnectionString_txbx.Text = String.Format("User id={0};Host={1};Database={2};password={3}",Login_txbx.Text.Trim(),DataSource_txbx.Text.Trim(), InitialDialog_txbx.Text.Trim(), Password_txbx.Text.Trim());
            }
        }
/// <summary>
/// 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        private void ChooseXMLFile_btn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openXML_fldg = new OpenFileDialog();
            openXML_fldg.FileName = string.Empty;
            openXML_fldg.Filter = "File XML (*.xml)|*.xml|All files (*.*)|*.*";

            if (openXML_fldg.ShowDialog() == DialogResult != true)
                XmlFile_txbx.Text = openXML_fldg.FileName;
        }
/// <summary>
/// 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        private void Connect_btn_Click(object sender, RoutedEventArgs e)
        {
            if (XML_rdbtn.IsChecked == true)
            {
                this.ConnectionParameters = this.XmlFile_txbx.Text.Trim();
                this.DBtype = SourceType.XML;
            }
            else
            {
                if (DataSource_txbx.Text.Trim().Length == 0 || InitialDialog_txbx.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Вы не указали сервер и базу данных");
                    return;
                }

                if (UseWinAuth_chbx.IsChecked == false)
                
                    if (Login_txbx.Text.Trim().Length == 0 || Password_txbx.Text.Trim().Length == 0)
                    {
                        MessageBox.Show("Вы не указали логин и пароль");
                        return;
                    }
                 ConnectionParameters = ConnectionString_txbx.Text.Trim();
                 DBtype = SourceType.SQL;
            }
           this.DialogResult = DialogResult!=null;
        }

    }
}
